package com.yicoollxing.controller.backend;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yicoollxing.common.Const;
import com.yicoollxing.common.ServerResponse;
import com.yicoollxing.pojo.Category;
import com.yicoollxing.pojo.User;
import com.yicoollxing.service.ICategoryService;
import com.yicoollxing.service.IUserService;


@Controller
@RequestMapping("/manager/category")
public class CategoryController {
    
	@Autowired
	private IUserService userService;
	
	@Autowired
	private ICategoryService iCategoryService;
	
	/**
	 * 
	 *添加菜类，修改分类信息（包括删除，修改类型，修改父菜单），删除菜类，搜索当前菜类的子菜类，搜索当前菜类的所有子菜类
	 * 
	 * 
	 */
	
	
	/**
	 *添加分类，默认主分类parentid=0
	 * @param categoryName
	 * @param parentId
	 * @param mid
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/add_category.do")
	public ServerResponse  addCategory(String categoryName,@RequestParam(value="parentId",defaultValue="0")Integer parentId,Integer mid,HttpSession session){
		
		User user=(User) session.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=userService.checkAdminRole(user);
		if(!response.isSuccess()){
			return  response;
		}
		
		return iCategoryService.addCategory(categoryName, parentId, mid);
	}
	
	@ResponseBody
	@RequestMapping("/del_category.do")
	public ServerResponse  delCategory(Integer categoryId,HttpSession session){
		
		User user=(User) session.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=userService.checkAdminRole(user);
		if(!response.isSuccess()){
			return  response;
		}
		
		return iCategoryService.delCategory(categoryId);
	}
	
	
	@ResponseBody
	@RequestMapping("/up_category.do")
	public ServerResponse  modifyCategory(Category category,HttpSession session){
		
		User user=(User) session.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=userService.checkAdminRole(user);
		if(!response.isSuccess()){
			return  response;
		}
		
		return iCategoryService.modifyCategory(category);
	}
	

	@ResponseBody
	@RequestMapping("/category_list.do")
	public ServerResponse  getCategoryListByCategoryId(Integer categoryId,HttpSession session){
		
		User user=(User) session.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=userService.checkAdminRole(user);
		if(!response.isSuccess()){
			return  response;
		}
		
		return iCategoryService.getCategoryListByCategoryId(categoryId);
	}
	
	
	/**
	 * 递归查询本节点的分类及孩子节点的分类
	 * @param categoryId
	 * @param session
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deep_category_list.do")
	public ServerResponse  getDeepCategoryListByCategoryId(Integer categoryId,HttpSession session){
		
		User user=(User) session.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=userService.checkAdminRole(user);
		if(!response.isSuccess()){
			return  response;
		}
		
		return iCategoryService.getDeepCategoryListByCategoryId(categoryId);
	}
	
	@ResponseBody
	@RequestMapping("/list.do")
	public ServerResponse list(Integer mid,@RequestParam(value="pageNum",defaultValue="1")Integer pageNum,
			@RequestParam(value="pageSize",defaultValue="10")Integer pageSize,HttpSession session){
		User user=(User) session.getAttribute(Const.CURRENT_USER);
		if(user==null){
			return ServerResponse.createByErrorMessage("请重新登录");
		}
		ServerResponse response=userService.checkAdminRole(user);
		if(!response.isSuccess()){
			return  response;
		}
		
		return iCategoryService.list(mid, pageNum, pageSize);
	}
	
}
